package cn.player.act;

import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.ref.WeakReference;

import cn.player.R;
import cn.player.tools.ShakeDetector;
import cn.player.tools.ShakeDetector.OnShakeListener;
import cntv.player.core.util.ToastUtil;

public class ShakeTestActivity extends BaseActivity {

    private ShakeDetector mShakeDetector;
    // 震动
    private Vibrator mVibrator;

    private int mClickCount, mShakeCount;

    private LinearLayout mLlRoot;
    private OnClickListener mOnClickListener;
    private int[] mColors = {R.color.red, R.color.lightblue, R.color.gray, R.color.lightyellow, R.color.lightgreen};

    private static final int MSG_SHAKE_COMPLETE = 0;
    private static final int SHAKE_LOADING_TIME = 2500;
    private ShakeHandler mShakeHandler = new ShakeHandler(this);
    //	protected boolean mShaking;
    private ImageView mIvShakeGesture;
    private LinearLayout mLlLoading;
    private TextView mTvShakeMessage;

    private static class ShakeHandler extends Handler {
        private WeakReference<Object> mReference;

        public ShakeHandler(Object loader) {
            mReference = new WeakReference<Object>(loader);
        }

        @Override
        public void handleMessage(Message msg) {
            Object object = mReference.get();
            ((ShakeTestActivity) object).handleMessage(msg);
        }
    }

    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_SHAKE_COMPLETE:
//			mShaking = false;
                mLlLoading.setVisibility(View.GONE);
                mTvShakeMessage.setVisibility(View.VISIBLE);
                ToastUtil.showToast(mContext, "跳转至历史记录页面去吧..", ToastUtil.LENGTH_SHORT);
                break;
            default:
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shake_test_activity);
        initializeData();
        findViews();
        setListeners();

    }

    private void initializeData() {
        // TODO Auto-generated method stub
        // 震动
        mVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
        mShakeDetector = new ShakeDetector(mContext);
    }

    private void findViews() {
        // TODO Auto-generated method stub
        mLlRoot = (LinearLayout) findViewById(R.id.ll_root_shake_test);
        mLlLoading = (LinearLayout) findViewById(R.id.ll_shake_loading);
        mIvShakeGesture = (ImageView) findViewById(R.id.iv_shake_gesture);
        mTvShakeMessage = (TextView) findViewById(R.id.tv_shake_message1);

    }

    private void animShakeGesture() {
        int relaSelf = Animation.RELATIVE_TO_SELF;
        RotateAnimation animation = new RotateAnimation(0f, -45f, relaSelf, 0f, relaSelf, 1f);
        animation.setDuration(350);
        mIvShakeGesture.startAnimation(animation);
    }

    private void setListeners() {
        // TODO Auto-generated method stub
        mOnClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mVibrator.vibrate(1000);
                mShakeHandler.removeMessages(MSG_SHAKE_COMPLETE);
                mShakeHandler.sendEmptyMessageDelayed(MSG_SHAKE_COMPLETE, SHAKE_LOADING_TIME);
            }
        };
        mShakeDetector.registerOnShakeListener(new OnShakeListener() {

            @Override
            public void onShake() {
                // TODO Auto-generated method stub
                mVibrator.vibrate(500);
                animShakeGesture();
                mLlLoading.setVisibility(View.VISIBLE);
                mTvShakeMessage.setVisibility(View.GONE);
                mShakeHandler.removeMessages(MSG_SHAKE_COMPLETE);
                mShakeHandler.sendEmptyMessageDelayed(MSG_SHAKE_COMPLETE, SHAKE_LOADING_TIME);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        boolean supportSensor = mShakeDetector.start();

        if (!supportSensor) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示").setMessage("您的手机不支持摇一摇功能!")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                            ShakeTestActivity.this.finish();
                        }
                    });
            builder.create().show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mShakeDetector.stop();
    }
}
